
const HIGH_SCORE = 'TETRIS.high-score';
const TOTAL_SCORE = 'TETRIS.total-score';
const TOTAL_GAMES = 'TETRIS.total-games';
const TIME_SPENT = 'TETRIS.time-spent';

/**
 * 数据中心，保存全局数据
 */
export default class DataCenter {
    private static score: number = 0;
    private static high: number = 0;
    private static total: number = 0;
    private static games: number = 0;
    private static timeSpent: number = 0;

    public static Row = 15;
    public static Col = 8;
    public static Width = 50;
    public static SPACING = 7;

    public static speed = 1;

    public static initDataCenter() {
        this.high = +localStorage.getItem(HIGH_SCORE) | 0;
        this.total = +localStorage.getItem(TOTAL_SCORE) | 0;
        this.games = +localStorage.getItem(TOTAL_GAMES) | 0;
        this.timeSpent = +localStorage.getItem(TIME_SPENT) | 0;

        console.log(`
当前最高分：${this.high},
当前  总分：${this.total},
当前总局数：${this.games},
当前总时间：${this.timeSpent}
        `);
    }

    public static addScore(score: number) {
        this.score += score;
        return this.score;
    }

    public static clearScore() {
        this.score = 0;
    }

    public static getScore() {
        return this.score;
    }

    public static setTimeSpent(timeSpent: number) {
        this.timeSpent += timeSpent;
        localStorage.setItem(TIME_SPENT, `${this.timeSpent}`);
    }

    public static getTime() {
        return this.timeSpent;
    }

    /**
     * 更新最大分值
     * @param high 最高分值
     */
    public static updateHigh() {
        const score = this.score;
        if (this.high < score) {
            this.high = score;
            localStorage.setItem(HIGH_SCORE, `${score}`);

            console.log(`NewHigh: ${score}`);
        }

        this.total += score;
        localStorage.setItem(TOTAL_SCORE, `${this.total}`);

        console.log(`NewTotal: ${this.total}`);

        this.games += 1;
        localStorage.setItem(TOTAL_GAMES, `${this.games}`);
    }

    public static getHigh() {
        return this.high;
    }

    public static getTotal() {
        return this.total;
    }

    public static getGames() {
        return this.games;
    }

    /**
     * 清除所有的记录
     */
    public static clearRecord() {
        this.games = 0;
        this.high = 0;
        this.total = 0;
        this.timeSpent = 0;

        localStorage.setItem(HIGH_SCORE, '0');
        localStorage.setItem(TOTAL_SCORE, '0');
        localStorage.setItem(TOTAL_GAMES, '0');
        localStorage.setItem(TIME_SPENT, '0');
    }
}
